package ua.khpi.si.kit49.diploma.controllers;

import java.util.Locale;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

import ua.khpi.si.kit49.diploma.core.Constants;
import ua.khpi.si.kit49.diploma.core.JsonRegistrationMessage;
import ua.khpi.si.kit49.diploma.db.dto.User;
import ua.khpi.si.kit49.diploma.service.RegistrationService;
import ua.khpi.si.kit49.diploma.util.RegistrationValidator;

/**
 * Controller is in charge of operation with registration and about page
 * 
 * @author netstalk3r
 * 
 */

@Controller
public class RegistrationAndAboutController {

	// //////////////////////////////////////////////////////////////////////
	// mapping for methods
	// //////////////////////////////////////////////////////////////////////
	private static final String ABOUT_PAGE_MAPPING = "/about";
	private static final String REGISTRATION_PAGE_MAPPING = "/registration";
	// //////////////////////////////////////////////////////////////////////

	/**
	 * Logger
	 */
	private static final Logger log = Logger
			.getLogger(RegistrationAndAboutController.class);

	/**
	 * validator for validate data of new user
	 */
	@Autowired(required = true)
	RegistrationValidator registrationValidator;

	/**
	 * service for register, ban, unban, remove users
	 */
	@Autowired(required = true)
	RegistrationService registrationService;

	/**
	 * method for getting about page
	 * 
	 * @param loc
	 *            current localization
	 * @return about page
	 */
	@RequestMapping(value = ABOUT_PAGE_MAPPING, method = RequestMethod.GET)
	public String getAboutPage(Locale loc) {
		log.info("Get about page");
		log.debug("Language - " + loc.getLanguage());
		return Constants.ABOUT_PAGE;
	}

	/**
	 * method for getting registration page
	 * 
	 * @param user
	 *            to register
	 * @param model
	 *            model map
	 * @return registration page
	 */
	@RequestMapping(value = REGISTRATION_PAGE_MAPPING, method = RequestMethod.GET)
	public String getRegistrationPage(@ModelAttribute("user") User user,
			ModelMap model) {
		log.info("get registration page");
		return Constants.REGISTRATION_PAGE;
	}

	/**
	 * method for register user
	 * 
	 * @param user
	 *            current user to register
	 * @param confirmPass
	 *            to check passwords
	 * @param loc
	 *            current localization
	 * @return message with information about errors or success
	 */
	@RequestMapping(value = REGISTRATION_PAGE_MAPPING, method = RequestMethod.POST)
	@ResponseBody
	public String registerUser(@ModelAttribute("user") User user,
			@RequestParam("conf-password") String confirmPass, Locale loc) {

		log.info("Registration controller work");
		log.debug("Language - " + loc.getLanguage());

		JsonRegistrationMessage jsonRegistrationMessage = new JsonRegistrationMessage();

		registrationValidator.validate(user, confirmPass,
				jsonRegistrationMessage, loc, true);

		if (jsonRegistrationMessage.isValid()) {
			registrationService.registerUser(user, false);
		}

		Gson gson = new Gson();

		String reply = gson.toJson(jsonRegistrationMessage);

		log.info("Reply: " + reply);

		log.info("Registration controller finished");
		return reply;
	}
}
